local Notify  = require("activity.Notify")
local ActivityExcel = require("excel.Activity")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local BagLogic = require("bag.BagLogic")
local ShopHelp = require("shop.ShopHelp")
local CopyExcel = require("excel.Map").copy
local VipLogic = require("vip.VipLogic")
local CopyHandler = require("copy.HandlerL")
local CopyCommon = require("copy.CopyCommon")
local FuliHandler = require("fuli.HandlerL")
local Log = require("common.Log")

MAX_ACT_VALUE_RECORD    = 32
ACT_VALUE_ID_DAILY_TASK    = 1
ACT_VALUE_ID_BOUNTY_TASK   = 2

--得到活跃度记录
function getActValueRecord(human, id)
  for i = 0, human.db.actValueRecordLen -1 do
      if human.db.actValueRecord[i].id == id then
          return human.db.actValueRecord[i]
      end
  end
end

function getActValueRecordCnt(human, id)
  local record = getActValueRecord(human,id)
  if record then
      return record.cnt
  end
  return 0
end

--增加活跃度
local function updateActValue(human, id)
  local config = ActivityExcel.actValue[id] 
  if not config then
      assert()
  end
  local record = getActValueRecord(human, id)
  if record == nil then
      if human.db.actValueRecordLen >= MAX_ACT_VALUE_RECORD then
          return
      end
      record = human.db.actValueRecord[human.db.actValueRecordLen]
      human.db.actValueRecordLen = human.db.actValueRecordLen + 1
      record.id = id
      record.cnt = 0
  end
  if record.cnt >= config.maxCnt then
      return
  end
  record.cnt = record.cnt + 1  
  
  human.db.actValue = human.db.actValue + config.valuePerCnt   
  
  Notify.CG_ACTIVITY_VALUE_UPDATE(human)    
end

-- 活跃度更新回调
function updateActivityValue(human,  type,  param)
   for k,v in pairs(ActivityExcel.actValue) do
      if v.type == type then
          local ok
          for i = 1,#v.param do
              if v.param[i] == param then
                  ok = true
                  break
              end
          end
          if param == nil or ok then
              updateActValue(human, k)
              break       
          end         
      end
   end
end


--完成活跃度
function finishActValue(human,  id)
   local config = ActivityExcel.actValue[id] 
   if not config then
      return
   end
   local record = getActValueRecord(human, id)
   if record and record.cnt >= config.maxCnt then
      return
   end
   if ObjHuman.getYuanbao(human) < config.yuanbao then
      ShopHelp.onNoRMB(human)
      return
   end   
   if record == nil then
      if human.db.actValueRecordLen >= MAX_ACT_VALUE_RECORD then
          return
      end
      record = human.db.actValueRecord[human.db.actValueRecordLen]
      human.db.actValueRecordLen = human.db.actValueRecordLen + 1
      record.id = id
      record.cnt = 0
  end
  ObjHuman.decYuanbao(human, config.yuanbao, "actvalue")
  human.db.actValue = human.db.actValue + config.valuePerCnt* (config.maxCnt - record.cnt)  
  record.cnt = config.maxCnt  
  Notify.CG_ACTIVITY_VALUE_UPDATE(human)   
  FuliHandler.checkShowTips(human) 
end

--领取活跃度奖励
function getActValueReward(human)
   local config
   local id 
   local min = 1000000
   for k,v in pairs(ActivityExcel.actValueReward) do
      if v.value > human.db.actValueRewardOk and v.value <= human.db.actValue then
          if v.value < min then
              config = v
              id = k
              min = v.value
          end
      end
   end
   if not config then
      return
   end
   local gridsCnt
   local grids
   gridsCnt,grids =  Notify.getRewardGridCnt(Notify.ActValueRewardCache, id)
   if BagLogic.getLeftGrids(human) < gridsCnt then
      Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.ACT_VAL_GET_REWARD_NO_BAG)
      return
   end
   
   for i = 1, gridsCnt do
      BagLogic.addItemByGrid(human, nil, grids[i], "actvalue")      
   end 
   human.db.actValueRewardOk = config.value
   Notify.CG_ACTIVITY_VALUE_UPDATE(human)   
   
   Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.ACT_VAL_GETREWARD_OK)   
   
   FuliHandler.checkShowTips(human)
   Log.write(Log.LOGID_OSS_ACTIVE_GET,human._id, human.account, human.name,human.db.lv,min)
end


function getCopyEnterCanAdd(human, copyID)
    if CopyExcel[copyID] == nil then
        return
    end 
    local config
    for k,v in pairs(ActivityExcel.copy) do
        if v.copyID == copyID then
            config = v
            break
        end 
    end 
    if not config then
        return
    end
    
    local cnt = VipLogic.getVipTimes(human, config.vipAdd)
    local addCnt = CopyCommon.getCopyVipAddCnt(human, copyID)
    cnt = cnt - addCnt
    if cnt < 0 then
        cnt = 0
    end
    return cnt    
end
function addCopyEnterCnt(human, copyID)
    if CopyExcel[copyID] == nil then
        return
    end
    local groupID = CopyExcel[copyID].group
    local config
    for k,v in pairs(ActivityExcel.copy) do
        if v.copyID == groupID then
            config = v
            break
        end 
    end 
    if not config then
        return
    end
    local cnt = VipLogic.getVipTimes(human, config.vipAdd)
    local addCnt = CopyCommon.getCopyVipAddCnt(human, copyID)
    if cnt == 0 then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TASK_ADD_CNT_NOT_VIP)
        return 
    end
    if cnt <= addCnt then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.COPY_PATA_ADD_FULL)
        return
    end
    if ObjHuman.getYuanbao(human) < config.vipAddCost then
      ShopHelp.onNoRMB(human)
      return
    end   
    ObjHuman.decYuanbao(human, config.vipAddCost , "copy_add") 
    CopyHandler.addVipCopyCnt(human, copyID)
    Notify.CG_ACTIVITY_COPY_UPDATE(human)
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.ACT_COPY_ADD_CNT_OK)       
end

function checkShowTips(human)
   for k,v in pairs(ActivityExcel.actValueReward) do
        if human.db.actValue < v.value then           
        elseif v.value > human.db.actValueRewardOk then
           return 1
        end
    end
    return 0
end
